﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Rotempco.BaseClass.Objects.Enums;
using System.Web.UI;
using System.ComponentModel;
using Rotempco.Website.Objects;
using Rotempco.BaseClass;
using System.Web.UI.WebControls;
using Rotempco.BaseClass.Objects.EntityObjects;
using Rotempco.Website.Logic;

namespace Rotempco.Website.UserControls
{
    [ParseChildren(false, "Text")]
    public class Translation : System.Web.UI.WebControls.WebControl
    {
        public Translation()
            : base()
        {
        }

        protected override void AddParsedSubObject(object obj)
        {
            if (obj is Button)
            {
                ((Button)obj).Text = Translate();
                base.AddParsedSubObject(obj);
            }
            if (obj is HyperLink)
            {
                ((HyperLink)obj).Text = Translate();
                base.AddParsedSubObject(obj);
            }
            if (obj is CheckBox)
            {
                ((CheckBox)obj).Text = Translate();
                base.AddParsedSubObject(obj);
            }
            else if (obj is LiteralControl)
            {
                Text = ((LiteralControl)obj).Text;
                ((LiteralControl)obj).Text = Translate();
                base.AddParsedSubObject(obj);
            }

        }

        public static string GetText(Guid guid, string text = null)
        {
            LanguageTypeEnum languageType = PublicSessionStore.Instance.LanguageType;

            if (languageType == LanguageTypeEnum.NA)
                languageType = LanguageLogic.GetDefaultLanguage();

            TextTranslation textTranslation = Factory.TextTranslation.GetSingle(languageType: languageType, guid: guid);

            if (textTranslation == null)
                return "<font color=\"red\">MISSING TRANSLATION : " + text + "</font>";
            else
                return textTranslation.Text;
        }

        private string Translate()
        {
            // Debug helper:
            if (SessionStore.Instance.DebugText)
                return Text;

            return GetText(Guid.Parse(key), Text);
        }

        private string key;
        public string Key
        {
            set
            {
                key = value;
            }

            get
            {
                return key;
            }
        }

        private string text;
        public string Text
        {
            get
            {
                return text;
            }

            set
            {
                text = value;
            }
        }


    }
}